﻿using ZyGames.DZPocker.Model;
using ZyGames.Framework.Game.Contract;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Tools;
using ZyGames.Framework.Common.Serialization;
using System;
using System.Collections.Generic;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 保存牌局下注记录
	/// </summary>
	public class Action30049 : BaseAction
	{
		private int tableId;
		private int userId;
		private bool result = false;
		private string log = string.Empty;
		private int tableHand;
		private TableData _currentTableData;
		private GameUser _currentGameUser;

		public Action30049(HttpGet actionGetter)
			: base((short)ActionType.DZSaveTableBetRecord, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("UserId", ref userId) &&
				httpGet.GetInt("TableHand", ref tableHand))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentTableData = GameRoom.Current.GetTableData(tableId);
			if (_currentTableData == null)
			{
				Tool.WriteWarmingLine("消息30049获得房间不存在");
				log = "消息异常";
				return true;
			}
			if(GameTable.Current.UserSavedCount(UserId) >= 15)
			{
				log = "超过存储上限";
				return true;
			}
			
			//判断是否这个玩家已经保存过
			_currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30049玩家不存在");
				log = "消息异常";
				return true;
			}
			if (!GameTable.Current.IsUserCanSaveBetRecord(_currentTableData.TableLogicID, tableHand, userId))
			{
				Tool.WriteErrorLine("消息30049已经添加过，不可重复保存牌局");
				log = "已经保存过不可重复保存";
				return true;
			}
			else
			{
				result = GameTable.Current.AddUserSavedBetRecord(_currentTableData.TableLogicID, tableHand, userId);
				if(!result)
				{
					log = "数据保存出现异常";
					return true;
				}
			}
			log = "保存成功";
			result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(result);
			this.PushIntoStack(log);
		}
	}
}
